package lock.lntelligence.gjx.activity;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import com.aiton.administrator.shane_library.shane.widget.ViewPagerIndicator;

import lock.lntelligence.gjx.R;
import lock.lntelligence.gjx.constant.Constant;
import lock.lntelligence.gjx.fragment.ManagerFragment;
import lock.lntelligence.gjx.fragment.OpenLockFragment;


public class DoActivity extends AppCompatActivity implements View.OnClickListener {

    private ViewPager mViewpager;
    private ViewPagerIndicator mViewPagerIndicator;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_do);
        if (Build.VERSION.SDK_INT >= 14) {
            //禁止横屏
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
        findID();
        initUI();
        setListener();
    }

    private void setListener() {
        mViewpager.addOnPageChangeListener(new MyPageChangerListener());
        findViewById(R.id.button_cancle).setOnClickListener(this);
    }

    private void initUI() {
        mViewpager.setAdapter(new MyAdapter(getSupportFragmentManager()));
    }

    private void findID() {
        mViewpager = (ViewPager) findViewById(R.id.viewpager);
        mViewPagerIndicator = (ViewPagerIndicator) findViewById(R.id.ViewPagerIndicator);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.button_cancle:
                finish();
                break;
        }
    }

    class MyPageChangerListener implements ViewPager.OnPageChangeListener
    {

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels)
        {
            mViewPagerIndicator.move( positionOffset,position);
        }

        @Override
        public void onPageSelected(int position)
        {

        }

        @Override
        public void onPageScrollStateChanged(int state)
        {

        }
    }

    class MyAdapter extends FragmentPagerAdapter{

        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            if (position==0){
                return new OpenLockFragment();
            }else if (position==1){
                return new ManagerFragment();
            }
            return null;
        }

        @Override
        public int getCount() {
            return 2;
        }
    }
    private boolean isSleep =false;
    public boolean isNotSleep =true;

    @Override
    protected void onStart() {
        super.onStart();
        isNotSleep = true ;
        if (isSleep){
            Intent intent = new Intent();
            intent.setClass(DoActivity.this,LockActivity.class);
            intent.putExtra("sleep","sleep");
            startActivityForResult(intent,Constant.REQUEST_RESULT.IS_SLEEP);
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (isNotSleep) {
            isSleep = true;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == Constant.REQUEST_RESULT.IS_SLEEP && resultCode == Constant.REQUEST_RESULT.IS_SLEEP){
            Log.e("onActivityResult ", "isSleep");
            isSleep = false;
        }
    }
}
